<?php


namespace api\modules\v1\controllers\buss;

use api\controllers\OnAuthController;
use api\modules\v1\forms\buss\ContentPublishRecordForm;
use common\helpers\CommonHelper;
use services\buss\ContentPublishRecordService;
use Yii;


class ContentPublishController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/buss/content-publish/list  内容数据列表
     * @apiName list
     * @apiGroup ContentPublish
     * @apiUse CommonParam
     *
     * @apiParam {String} publish_stime     发布开始时间【格式：YYYY-MM-DD】
     * @apiParam {String} publish_etime     发布结束时间【格式：YYYY-MM-DD】
     * @apiParam {String} keywords          关键词
     * @apiParam {Number} platform_id       平台ID
     * @apiParam {Number} doctor_id         医生ID
     * @apiParam {Number} created_member_id 创建人ID
     * @apiParam {String} order_column      排序字段
     * @apiParam {String} order_sort        排序值【asc正序、desc倒序】
     * @apiParam {String} page              页码
     * @apiParam {String} pageSize          每页展示数
     *
     * @apiSuccess {Object}  data                       返回数据
     * @apiSuccess {Number}  data.page                  页码
     * @apiSuccess {Number}  data.pageSize              每页展示数
     * @apiSuccess {Number}  data.total                 总数
     * @apiSuccess {Array}   data.list                  列表数据
     * @apiSuccess {Number}  data.list.id               发布ID
     * @apiSuccess {Number}  data.list.content_id       内容ID
     * @apiSuccess {String}  data.list.title            标题
     * @apiSuccess {String}  data.list.platform_name    平台
     * @apiSuccess {String}  data.list.account_name     账号
     * @apiSuccess {String}  data.list.doctor_name      医生
     * @apiSuccess {String}  data.list.read_num         阅读
     * @apiSuccess {String}  data.list.like_num         点赞
     * @apiSuccess {String}  data.list.collect_num      收藏
     * @apiSuccess {String}  data.list.comment_num      评论
     * @apiSuccess {String}  data.list.created_member_name  创建人
     * @apiSuccess {String}  data.list.publish_time_str 发布时间
     * @apiSuccess {String}  data.list.change_date      内容更新时间
     * @apiSuccess {String}  data.list.is_edit          是否已录入【1是、0否】
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        $service = new ContentPublishRecordService();
        // 查询结果
        $data = $service->getListPage($params, (int)$page, (int)$pageSize);
        // 格式化数据
        $data['list'] = $service->getListPageFormat($data['list']);
        return $data;
    }

    /**
     * @api {get} /v1/buss/content-publish/list-condition  列表筛选条件
     * @apiName list-condition
     * @apiGroup ContentPublish
     * @apiUse CommonParam
     *
     * @apiSuccess {Object}  data                   返回数据
     * @apiSuccess {Array}   data.project           项目【{value:'0', label: '项目'}】
     * @apiSuccess {Array}   data.publist_status    发布状态【{value:'0', label: '普通'}】
     * @apiSuccess {Array}   data.type              类型【{value:'0', label: '普通'}】
     * @apiSuccess {Array}   data.created_member    创建人
     * @apiSuccess {Array}   data.tag               标签
     * @apiSuccess {Array}   data.classify          分类
     * @apiSuccess {Array}   data.department_arr    部门列表
     */
    public function actionListCondition()
    {
        $params = Yii::$app->request->get();

        $service = new ContentPublishRecordService();
        return $service->getSelectCondition();
    }

    /**
     * @api {get} /v1/buss/content-publish/info 内容数据详情
     * @apiName info
     * @apiGroup ContentPublish
     * @apiUse CommonParam
     *
     * @apiParam {Number} id 内容ID【新增ID不传，编辑必传ID】
     *
     * @apiSuccess {Object} data                    返回数据
     * @apiSuccess {Number} data.id                 ID
     * @apiSuccess {String} data.read_num           浏览量
     * @apiSuccess {String} data.like_num           点赞
     * @apiSuccess {String} data.collect_num        收藏
     * @apiSuccess {String} data.comment_num        评论
     */
    public function actionInfo()
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;

        // 查询结果
        $service = new ContentPublishRecordService();
        return $service->getInfo((int)$id);
    }

    /**
     * @api {post} /v1/buss/content-publish/create 新增内容数据
     * @apiName publish
     * @apiGroup ContentPublish
     * @apiUse CommonParam
     *
     * @apiBody {Number} content_id         内容ID
     * @apiBody {Number} doctor_id          医生ID
     * @apiBody {Array}  account_ids        账号IDs
     * @apiBody {Date} publish_time         发布时间
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         内容ID
     */
    public function actionCreate()
    {
        $params = Yii::$app->request->post();

        $model = new ContentPublishRecordForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $service = new ContentPublishRecordService();
        $service->create($model);
        return true;
    }

    /**
     * @api {post} /v1/buss/content-publish/update-data 编辑录入数据
     * @apiName update-data
     * @apiGroup ContentPublish
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                内容ID
     * @apiBody {String} read_num          浏览量
     * @apiBody {String} like_num          点赞数
     * @apiBody {String} collect_num       收藏数
     * @apiBody {String} comment_num       评论数
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         内容ID
     */
    public function actionUpdateData()
    {
        $params = Yii::$app->request->post();

        $model = new ContentPublishRecordForm();
        $model->setScenario('edit_data');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $service = new ContentPublishRecordService();
        return $service->updateData($model);
    }

    /**
     * @api {post} /v1/buss/content-publish/delete 删除录入数据
     * @apiName delete
     * @apiGroup ContentPublish
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                内容ID
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         内容ID
     */
    public function actionDelete()
    {
        $params = Yii::$app->request->post();

        $model = new ContentPublishRecordForm();
        $model->setScenario('delete');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $service = new ContentPublishRecordService();
        return $service->delete($model);
    }
}
